library(tree)
setwd("D:\\workspace\\DevWork\\example\\GitRDemo\\RDemo\\0001分类与回归树")
bj <- read.csv("bjGDP.csv")
View(bj)
plot(bj$year,bj$gdp)
bj.lm <- lm(gdp~year,data=bj)
plot(bj$year,bj$gdp)
abline(bj.lm,col="red",lwd=1)
text(1997,14000,paste("Adjusted R-squared: ",summary(bj.lm)$adj.r.squared))

bj.tree <- tree(gdp~year,data=bj)
summary(bj.tree)
plot(bj.tree)
text(bj.tree)

plot(bj$year,bj$gdp)
abline(v=2006.5)
abline(v=2000.5)

bj2 <- subset(bj,year> 2006.5)
summary(bj2)
bj2.lm <- lm(gdp~year,data=bj2)
plot(bj2$year,bj2$gdp)
abline(bj2.lm,col="red",lwd=1)
text(2011,14000,paste("Adjusted R-squared: ",summary(bj2.lm)$adj.r.squared))
summary(bj2.lm)

bj3 <- subset(bj,year> 2000.5 & year < 2006.5)
summary(bj3)
bj3.lm <- lm(gdp~year,data=bj3)
plot(bj3$year,bj3$gdp)
abline(bj3.lm,col="red",lwd=1)
text(2004,6000,paste("Adjusted R-squared: ",summary(bj3.lm)$adj.r.squared))
summary(bj3.lm)

bj4 <- subset(bj,year< 2000.5)
summary(bj4)
bj4.lm <- lm(gdp~year,data=bj4)
plot(bj4$year,bj4$gdp)
abline(bj4.lm,col="red",lwd=1)
text(1990,2000,paste("Adjusted R-squared: ",summary(bj4.lm)$adj.r.squared))
summary(bj4.lm)


plot(bj$year,bj$gdp)
abline(bj.lm,col="red",lwd=1,lty=5)
text(1999,9000,"全局回归线",col="red")
abline(v=2006.5,lty=2)
text(2006.6,22200,">2006.5")
abline(v=2000.5,lty=2)
text(2000.5,22200,">2000.5")
abline(bj2.lm,col="green",lwd=1)
text(2010,16000,"2006年之后回归线",col="green")
abline(bj3.lm,col="blue",lwd=1)
text(2012,11000,"2001-2006\n年回归线",col="blue")
abline(bj4.lm,col="orange",lwd=1)
text(1990,2000,"2001年之前回归线",col="orange")
